﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nido.Common.BackEnd;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using Nido.Common.Utilities.Attributes;

namespace DemoTest.Bll.Models
{
    /// <summary>
    /// Created by MAS IT
    /// </summary>
    [Auditable]
    public class Student : BaseObject
    {
		public Student()
			// Change this parameter to change the DisplayName 
			// (this name is used in all system messages)
			// property of this object
			: base("Student") { }

		#region Public Properties
		[Key]
		[Nido.Common.Utilities.Validators.Required]
		[DisplayName("Student Id")]
		public int StudentId { get; set; }

		[Nido.Common.Utilities.Validators.Required]
		[ForeignKey("OfficeAddress")]
		[DisplayName("Office Address Id")]
		[ScaffoldColumn(false)]
        [Auditable]
		public int OfficeAddressId { get; set; }

		[DisplayName("Office Address")]
		[ScaffoldColumn(false)]
		public Address OfficeAddress { get; set; } 

		[Nido.Common.Utilities.Validators.Required]
		[ForeignKey("HomeAddress")]
		[DisplayName("Home Address Id")]
		[ScaffoldColumn(false)]
		public int HomeAddressId { get; set; }

		[DisplayName("Home Address")]
		[ScaffoldColumn(false)]
		public Address HomeAddress { get; set; } 

		[Nido.Common.Utilities.Validators.Required]
		[Nido.Common.Utilities.Validators.StringLength(50)]
		[DisplayName("Student Name")]
        [Auditable]
        [Encrypted]
		public string Name { get; set; }

		[Nido.Common.Utilities.Validators.Required]
		[DisplayName("Age")]
		public int Age { get; set; }

		[Nido.Common.Utilities.Validators.Required]
		[Nido.Common.Utilities.Validators.StringLength(150)]
		[DisplayName("Address")]
        [Encrypted]
		public string Address { get; set; }

		[Nido.Common.Utilities.Validators.Required]
		[DisplayName("Registered Date")]
		public DateTime RegisteredDate { get; set; }

		[DisplayName("Is Active")]
		public bool? IsActive { get; set; }

		[Nido.Common.Utilities.Validators.StringLength(250)]
		[DisplayName("Description")]
		public string Description { get; set; }

		[DisplayName("Student Courses")]
		[ScaffoldColumn(false)]
		public ICollection<StudentCourse> StudentCourses { get; set; } 

		[DisplayName("Datas")]
		[ScaffoldColumn(false)]
		public ICollection<Data> Datas { get; set; } 

		#endregion

		#region Not Mapped Properties

		[NotMapped]
        public string OfficeAddressPopup
        {
            get
            {
                if (this.OfficeAddress != null)
                    return this.CreatePopupText(this.OfficeAddress.Text
                        , new TableCreator<Address>(this.OfficeAddress)
                        .RemoveRecord(x => x.Value).RemoveRecord(x => x.Text)
                        .RemoveRecord(x => x.DeleteError).RemoveRecord(x => x.CanDelete)
                        .GetPopupTable());
                else
                    return "Indecisive record data!!";
            }
        }

		[NotMapped]
        public string HomeAddressPopup
        {
            get
            {
                if (this.HomeAddress != null)
                    return this.CreatePopupText(this.HomeAddress.Text
                        , new TableCreator<Address>(this.HomeAddress)
                        .RemoveRecord(x => x.Value).RemoveRecord(x => x.Text)
                        .RemoveRecord(x => x.DeleteError).RemoveRecord(x => x.CanDelete)
                        .GetPopupTable());
                else
                    return "Indecisive record data!!";
            }
        }

		/// <summary>
        /// Implement this to prevent or allow the object to be deleted through the base handler.
        /// You need to consider all dependant objects before allowing a object to be deleted.
        /// Therefore please implement this correctly/ accordingly.
        /// </summary>
		[NotMapped] 
		[ScaffoldColumn(false)]
		public override bool CanDelete
        {
            // TODO: Impletement this 
            // Example: return ((StudentCourses != null) && (StudentCourses.Count > 0)) ? false : true;
            get
            {
                if (_canDelete.HasValue)
                    return _canDelete.Value;
                else
                    return !(((StudentCourses != null) 
					 && (StudentCourses.Count > 0)) || ((Datas != null) 
					 && (Datas.Count > 0)));
            }
            set { _canDelete = value; }
        }
        private bool? _canDelete;

		[NotMapped]
		[ScaffoldColumn(false)]
		public override string DeleteError
		{
			get 
			{ 
				return "<b>" + ((StudentCourses != null) ? StudentCourses.Count.ToString() : "Indecisive # of") + "</b> StudentCourse(s) are using this 'Student'<br/>"
					 + "<b>" + ((Datas != null) ? Datas.Count.ToString() : "Indecisive # of") + "</b> Data(s) are using this 'Student'<br/>"; 
			} 
		}
		
		[NotMapped]
		[ScaffoldColumn(false)]
		public override string DeleteErrorText
		{
			get 
			{ 
				return ((StudentCourses != null) ? StudentCourses.Count.ToString() : "Indecisive # of")
					 + " StudentCourse(s), " + ((Datas != null) ? Datas.Count.ToString() : "Indecisive # of")
					 + " Data(s) are using this 'Student'";
			} 
		}

		/// <summary>
        /// This property set the value of the items of the combo box that created 
		/// deriving the BaseCombo. You may change this as needed.
        /// </summary>
        [NotMapped]
		[ScaffoldColumn(false)]
        public override string Value 
		{ get { return Convert.ToString(StudentId); } set { StudentId = Convert.ToInt32(value); } }

		/// <summary>
        /// This property set the display name of the items of the combo box that created 
		/// deriving the BaseCombo. You may modify or delete this as required.
        /// </summary>
        [NotMapped]
		[ScaffoldColumn(false)]
        public override string Text { get { return Name; } set { Name = value; } }
		
		#endregion
    }
}
